#include <REGX52.H>
#include "Delay.h"
#include "oled.h"
#include "DS1302.h"
#include "DS18B20.h"
#include "Buzzer.h"

unsigned char TimeSetFlashFlag;
unsigned char MODE0;
unsigned char TimeSetSelect;
unsigned char Buzzer,Sw=0;
float T, TShow;
sbit Motor = P1^0;
extern char Clock_Time[] = {0,0,26,0};//时，分，温度，秒

/**
  * @brief  光标归零
  */
void TimeSetSelect0()
{
	TimeSetSelect = 0;
}


/**
  * @brief  闹钟和温度设置显示
  */
void Clock_Show()
{
		OLED_ShowString(0,0,"Clock:",16);
		OLED_ShowNum(56,0,Clock_Time[0],2,16);
		OLED_ShowChar(72,0,':',16);
		OLED_ShowNum(80,0,Clock_Time[1],2,16);
		if(Sw == 0)
		{
			OLED_ShowString(111,0,"Y",16);
		}
		else
		{
			OLED_ShowString(111,0,"N",16);
		}
		OLED_ShowString(0,2,"AlarmTemp:",16);
		OLED_ShowNum(88,2,Clock_Time[2],2,16);
		OLED_ShowChar(104,2,'z'+2,8);
		OLED_ShowChar(112,2,'C',16);
}

/**
  * @brief  闹钟响铃
  * @param  KeyNum 按键值
  * @retval 无
  */
void Clock_Ring(unsigned char KeyNum)
{
	if(Clock_Time[0]==DS1302_Time[3]&&Clock_Time[1]==DS1302_Time[4] && Clock_Time[3]==DS1302_Time[5]&&Sw==0)
	{
		Buzzer = 1;
	}
	//关闭闹钟
	if(KeyNum == 6 )
	{
		Buzzer = 0;
	}
	Buzzer_Time(Buzzer);	
}


/**
  * @brief 闹钟和温度报警值设置 
  * @param  KeyNum按键值
  * @retval 无
  */
void Clock_Set(unsigned char KeyNum)
{
	
	//闹钟开关
	if(KeyNum == 8)
	{
		Sw = ~Sw;
	}
	if(KeyNum == 2)
	{
		TimeSetSelect++;
		TimeSetSelect%=3;
	}
	
	//前移动光标
	if(KeyNum == 3)
	{
		TimeSetSelect--;
		if(TimeSetSelect<0 ||TimeSetSelect>10)
		{
				TimeSetSelect = 2;
		}
	}
		OLED_ShowNum(104,6,TimeSetSelect,1,8);
	//加
	if(KeyNum == 4)
	{
		Clock_Time[TimeSetSelect]++;
		if(Clock_Time[0]>23){Clock_Time[0]=0;}
		if(Clock_Time[1]>59){Clock_Time[1]=0;}
		if(Clock_Time[2]>99){Clock_Time[2]=99;}
	}
		//按键3，减
	if(KeyNum == 5)
	{
		Clock_Time[TimeSetSelect]--;
		if(Clock_Time[0]<0){Clock_Time[0]=59;}
		if(Clock_Time[1]<0){Clock_Time[1]=59;}
		if(Clock_Time[2]<0){Clock_Time[1]=99;}
	}

	//进行设置显示，让被选中的进行闪烁
	if(TimeSetSelect == 0 && TimeSetFlashFlag == 1){OLED_ShowString(56,0,"  ",16);}
	else if(TimeSetSelect == 1 && TimeSetFlashFlag == 1){OLED_ShowString(80,0,"  ",16);}
	else if(TimeSetSelect == 2 && TimeSetFlashFlag == 1){OLED_ShowString(88,2,"  ",16);}
	else {Clock_Show();}
}



/**
  * @brief  温度显示，归于模式0显示
  */
void T_Show()
{
		DS18B20_ConvertT();	
		T = DS18B20_ReadT();	
		if(T<0)
		{
			TShow = -T;
		}
		else
		{
			TShow = T;
		}
		OLED_ShowNum(56,5,TShow,2,16);
		OLED_ShowChar(72,5,'z'+2,8);
		OLED_ShowChar(80,5,'C',16);
		//温度报警
		if(T > Clock_Time[2] && Sw==0)
		{
//			Motor=1;
			P2=0;
		}
		else
		{
//			Motor=0;
			P2=0xFF;
		}
}


/**
  * @brief  回到模式0，在保存更改时间时调用
  * @param  无
  * @retval 无
  */
unsigned char GetMODE0()
{
	return MODE0;
}


/**
  * @brief  时间显示，模式0
  * @param  KeyNum按键值
  */
void TimeShow(void)
{
//		DS1302_ReadTime();
		OLED_ShowNum(8,0,20,2,16);//年
		OLED_ShowNum(24,0,DS1302_Time[0],2,16);//月
		OLED_ShowChinese(40,0,0,16);
		OLED_ShowNum(56,0,DS1302_Time[1],2,16);
		OLED_ShowChinese(72,0,1,16);
		OLED_ShowNum(88,0,DS1302_Time[2],2,16);
		OLED_ShowChinese(104,0,2,16);
		
		//时分秒
		OLED_ShowNum(24,2,DS1302_Time[3],2,16);
		OLED_ShowChar(44,2,':',16);
		OLED_ShowNum(56,2,DS1302_Time[4],2,16);
		OLED_ShowChar(76,2,':',16);
		OLED_ShowNum(88,2,DS1302_Time[5],2,16);		
		//星期
		OLED_ShowChinese(0,5,3,16);
		OLED_ShowChinese(16,5,4,16);
		OLED_ShowChinese(32,5,WeekSet()%7+5,16);
	
		T_Show();//温度显示		
}


/**
  * @brief  模式1,时间设置
  * @param  KeyNum按键值
  */
void TimeSet(unsigned char KeyNum)
{
	//按下按键二，后移动光标,其中星期能设置，但是会自动匹配
	if(KeyNum == 2)
	{
		TimeSetSelect++;
		TimeSetSelect%=6;
//		LCD_ShowNum(2,14,TimeSetSelect,1);
	}
	
	//前移动光标
	if(KeyNum == 3)
	{
		TimeSetSelect--;
		if(TimeSetSelect<0 ||TimeSetSelect>10)
		{
				TimeSetSelect = 5;
		}
//		LCD_ShowNum(2,14,TimeSetSelect,1);
	}
	
	//值加
	if(KeyNum == 4)
	{
		DS1302_Time[TimeSetSelect]++;
		if(DS1302_Time[0]>99){DS1302_Time[0]=0;}
		if(DS1302_Time[1]>12){DS1302_Time[1]=1;}
		if(DS1302_Time[1]==1 || DS1302_Time[1]==3 || DS1302_Time[1]==5 || DS1302_Time[1]==7 || DS1302_Time[1]==8
			 || DS1302_Time[1]==10 || DS1302_Time[1]==12)
		{
			if(DS1302_Time[2]>31){DS1302_Time[2]=1;}
			if(DS1302_Time[2]<1){DS1302_Time[2]=31;}
		}
		else if(DS1302_Time[1]==4 || DS1302_Time[1]==6 || DS1302_Time[1]==9 || DS1302_Time[1]==11)
		{
			if(DS1302_Time[2]>30){DS1302_Time[2]=1;}
			if(DS1302_Time[2]<1){DS1302_Time[2]=30;}
		}
		else if(DS1302_Time[1]==2)
		{
			if((DS1302_Time[0]%4==0 && DS1302_Time[0]%100!=0)|| DS1302_Time[0]%400==0)
			{
				if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
				if(DS1302_Time[2]<1){DS1302_Time[2]=29;}
			}
			else
			{
				if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
				if(DS1302_Time[2]<1){DS1302_Time[2]=28;}
			}
		}
		if(DS1302_Time[3]>23){DS1302_Time[3]=0;}
		if(DS1302_Time[4]>59){DS1302_Time[4]=0;}
		if(DS1302_Time[5]>59){DS1302_Time[5]=0;}
	}
	
	
	
	//值减
	if(KeyNum == 5)
	{
		DS1302_Time[TimeSetSelect]--;
		if(DS1302_Time[0]<0){DS1302_Time[0]=99;}
		if(DS1302_Time[1]<1){DS1302_Time[1]=12;}
		if(DS1302_Time[1]==1 || DS1302_Time[1]==3 || DS1302_Time[1]==5 || DS1302_Time[1]==7 || DS1302_Time[1]==8
			 || DS1302_Time[1]==10 || DS1302_Time[1]==12)
		{
			if(DS1302_Time[2]<1){DS1302_Time[2]=31;}
			if(DS1302_Time[2]>31){DS1302_Time[2]=1;}
		}
		else if(DS1302_Time[1]==4 || DS1302_Time[1]==6 || DS1302_Time[1]==9 || DS1302_Time[1]==11)
		{
			if(DS1302_Time[2]<1){DS1302_Time[2]=30;}
			if(DS1302_Time[2]>30){DS1302_Time[2]=1;}
		}
		else if(DS1302_Time[1]==2)
		{
			if((DS1302_Time[0]%4==0 && DS1302_Time[0]%100!=0)|| DS1302_Time[0]%400==0)
			{
				if(DS1302_Time[2]<1){DS1302_Time[2]=29;}
				if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
			}
			else
			{
				if(DS1302_Time[2]<1){DS1302_Time[2]=28;}
				if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
			}
		}
		if(DS1302_Time[3]<0){DS1302_Time[3]=23;}
		if(DS1302_Time[4]<0){DS1302_Time[4]=59;}
		if(DS1302_Time[5]<0){DS1302_Time[5]=59;}
	}
	//自动匹配星期
	WeekSet();
	
	

	//进行设置显示，让被选中的进行闪烁
	if(TimeSetSelect == 0 && TimeSetFlashFlag == 1){OLED_ShowString(24,0,"  ",16);}	
	else if(TimeSetSelect == 1 && TimeSetFlashFlag == 1){OLED_ShowString(56,0,"  ",16);}
	else if(TimeSetSelect == 2 && TimeSetFlashFlag == 1){OLED_ShowString(88,0,"  ",16);}
	else if(TimeSetSelect == 6 && TimeSetFlashFlag == 1){OLED_ShowString(32,5,"  ",16);}
	else if(TimeSetSelect == 3 && TimeSetFlashFlag == 1){OLED_ShowString(24,2,"  ",16);}	
	else if(TimeSetSelect == 4 && TimeSetFlashFlag == 1){OLED_ShowString(56,2,"  ",16);}	
	else if(TimeSetSelect == 5 && TimeSetFlashFlag == 1){OLED_ShowString(88,2,"  ",16);}
	else{TimeShow();}
	
	//确认修改
	if(KeyNum == 7)
	{
		DS1302_SetTime();
		MODE0 = 1;
	}
	else
	{
		MODE0 = 0;
	}
}


/**
  * @brief  通过定时器0进行光标闪烁
  */
unsigned char TimeSetFlash()
{
	TimeSetFlashFlag = !TimeSetFlashFlag;
	return TimeSetFlashFlag;
}

